/**
 * 
 */
package trungsi.gea.photos.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import trungsi.gea.photos.dao.FolderDao;
import trungsi.gea.photos.domain.Folder;
import trungsi.gea.photos.domain.Photo;

/**
 * @author trungsi
 *
 */
@Service
public class FolderService {

	@Autowired
	private FolderDao folderDao;
	@Autowired
	private PhotoService photoService;
	//@Autowired
	//private PersistenceManagerHolder pmHolder;
	
	public void save(Folder folder) {
		if (folderDao.findByName(folder.getName()) != null) {
			throw new RuntimeException("folder with name " + folder.getName() + " already existed");
		}
		folderDao.save(folder);
	}
	
	public List<Folder> findAll() {
		return folderDao.findAll();
	}
	
	public void delete(Folder folder) {
		photoService.deleteAll(folder);
		folderDao.delete(folder);
	}
	
	public void deleteAll() {
		List<Folder> folders = folderDao.findAll();
		for (Folder folder : folders) {
			delete(folder);
		}
	}

	public Photo getFirstPhoto(Folder folder) {
		return photoService.getFirstPhoto(folder);
	}

	public List<Folder> findAll(Date before, int limit) {
		return folderDao.findAll(before, limit);
	}

	public List<Folder> findAll(int limit) {
		return folderDao.findAll(limit);
	}

	public int countAll() {
		return folderDao.countAll();
	}
}
